<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/6/17
 * Time: 11:49
 */

namespace app\api\controller\v27;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\AppConfig;
use app\common\cachemodel\Goods;
use app\common\cachemodel\live\LiveGoodsActivity;
use app\common\cachemodel\live\LiveRecordingInfo;
use app\common\cachemodel\MallGoods;
use app\common\cachemodel\MallGoodsAttr;
use app\common\cachemodel\MallGoodsTags;

/**
 * @version 2.7.0
 * Class PhysicalStore
 * @package app\api\controller\v20
 */
class MallStore extends Auth2
{
    /**
     * 实体商品详情接口
     * @return \think\response\Json
     * @throws \Exception
     */
    public function goods_detail()
    {
        $gid = input('gid/d', 0);
        $liveId = input('live_id/d', 0);
        $liveRecordId = input('live_record_id/d', 0);
        (new LiveRecordingInfo())->checkRecordId($liveId, $liveRecordId);

        $mallGoodsModel = new MallGoods();
        $goods = current($mallGoodsModel->batGetInfo([$gid], ['name', 'imgs', 'now_price', 'vip_price', 'status']));//获取当前商品的信息
        if ($goods)
        {
            $status = $goods['status'] ? 1 : 0;
            $goodsContent = $mallGoodsModel->getGoodsContent($gid);//获取商品图文信息

            $spectInfo = (new MallGoodsAttr())->getGoodsAttrList($gid, ['name', 'id', 'now_price', 'vip_price', 'inventory', 'img']);//获取商品的规格信息

            $tags = (new MallGoodsTags())->getGoodsTags($gid, ['name']);//获取商品的标签信息
            $tags = $tags ? array_column($tags, 'name') : [];

            $allGoodsIds = $mallGoodsModel->getGoodsList();//获取所有的商品id
            $newGoodsIds = [];
            if ($allGoodsIds)
            {
                $notDisplayGids = [];
                if ($this->is_apple_check)
                {   // 苹果审核禁止 显示商品
                    $notDisplayGoodsConfig = (new AppConfig())->configInfo('apple_check');
                    $notDisplayGids = isset($notDisplayGoodsConfig['mall_not_display_goods']) && !empty($notDisplayGoodsConfig['mall_not_display_goods']) ? explode(',', $notDisplayGoodsConfig['mall_not_display_goods']) : [];
                }

                $notDisplayGids[] = $gid;
                $newGoodsIds = array_diff($allGoodsIds, $notDisplayGids);
            }
            $newGoodsInfo = $mallGoodsModel->batGetInfo(array_slice($newGoodsIds, 0, 6), ['id', 'imgs', 'name', 'now_price']);//获取不包含当前商品的六个商品信息
            //重组推荐商品数据格式
            $recommendGoods = [];
            foreach ($newGoodsInfo as $k => $good)
            {
                $img = $good['imgs'] ? explode(',', $good['imgs'])[0] : '';
                $recommendGoods[] = [
                    'img' => $img ? $img . '?x-oss-process=style/scale0.2' : '',
                    'gid' => (int)$good['id'],
                    'title' => $good['name'],
                    'price' => $good['now_price']
                ];
            }
            if ($goods['vip_price'] > 0)
            {   // 设置了会员价显示
                $vipPrice = (string)$goods['vip_price'];
            } else
            {   // 没有设置会员价显示
                $vipPrice = null;
            }
            $arr = [
                'img_list' => $goods['imgs'] ? explode(',', $goods['imgs']) : [],
                'title' => $goods['name'],
                'content' => $goodsContent,
                'price' => '',
                'vip_price' => $vipPrice,
                'vip_desc' => '行星会员价',
                'tag_list' => $tags,
                'status' => $status,
                'recommend_goods' => $recommendGoods,
                'recommend_title' => '「万幸」精选好物'
            ];
            $spectInfo = array_column($spectInfo, null, 'id');
            //打折已商品现价为基础,不使用vip价格
            if ($liveId)
            {
                (new LiveGoodsActivity())->getLiveOrderTypeGoods($liveId, 14, $spectInfo);
            }
            $spectInfoTmp = (new Goods())->resetGoodsInfo2($this, 14, $spectInfo, 0, 0, '', [$gid], ['isGoodInfo' => true]);
            $spect_list = [];
            foreach ($spectInfoTmp['goods_data'] as $item)
            {
                $tmp = [
                    'title' => $item['name'],
                    'price' => $item['price'],//现价
                    'vip_price' => $spectInfo[$item['gid']]['vip_price'],
                    'vip_desc' => '行星会员价',
                    'num' => (int)($status ? $spectInfo[$item['gid']]['inventory'] : 0),
                    'img' => $spectInfo[$item['gid']]['img'],
                    'id' => (int)$item['gid'],
                    'have_live_active' => 0
                ];

                if (!empty($item['active_data']))
                {
                    $tmp['vip_price'] = $item['active_data']['now_price'];
                    if ($item['active_data']['type'] == 1)
                    {
                        $tmp['vip_desc'] = '官方活动';
                    } else
                    {
                        $tmp['vip_desc'] = '直播间活动';
                        $tmp['have_live_active'] = 1;
                    }
                }
                $spect_list[] = $tmp;
            }
            $arr['spect_list'] = $spect_list;
            $arr['price'] = current($spect_list)['price'];
            $arr['vip_price'] = current($spect_list)['vip_price'];
            $arr['vip_desc'] = current($spect_list)['vip_desc'];
            return show(config('code.success'), '获取数据成功', $arr);
        }
        return show(config('code.error'), '商品不存在');
    }
}
